﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DingTalkAgent.Web.Models
{
    /// <summary>
    /// 角色对应功能对应表
    /// </summary>
    public class RoleToken
    {
        public static List<Model.role_action> list_role_action = new List<Model.role_action>();

        /// <summary>
        /// 全局更新角色功能
        /// </summary>
        public static void updateRoleList()
        {
            SqlData.Roles dh = new SqlData.Roles();
            //
            list_role_action = dh.queryRoleList();
        }
        /// <summary>
        /// 返回页面可操作项
        /// </summary>
        /// <param name="controller">controller</param>
        /// <param name="action">action</param>
        /// <returns></returns>
        public static List<string> queryPageAction(int RoleID, string controller, string action)
        {
            if (list_role_action.Count() == 0)
            {
                updateRoleList();
            }
            var actionlist = new List<string>();
            var r = list_role_action.Where(m => { return m.RoleID == RoleID && m.Controller.ToLower() == controller.ToLower() && m.Action.ToLower() == action.ToLower(); });
            if (r.Count() > 0)
            {
                var x = list_role_action.Where(m => { return m.RoleID == RoleID && m.Page_ID == r.FirstOrDefault().Page_ID; });
                x.ToList().ForEach(a => {
                    actionlist.Add(a.Action.ToLower());
                });
            }
            return actionlist;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static bool queryAction(int RoleID, string controller, string action)
        {
            if (list_role_action.Count() == 0)
            {
                updateRoleList();
            }
            var actionlist = new List<string>();
            var r = list_role_action.Where(m => { return m.RoleID == RoleID && m.Controller.ToLower() == controller.ToLower() && m.Action.ToLower() == action.ToLower(); });
            if (r.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}